package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Member;
import com.itheima.pojo.Setmeal;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 会员服务
 */
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private SetmealDao setmealDao;


    //根据手机号查询会员信息
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    //新增会员
    public void add(Member member) {
        //如果用户设置了密码，需要对密码进行md5加密
        String password = member.getPassword();
        if(password != null){
            password = MD5Utils.md5(password);
            member.setPassword(password);
        }
        Date regTime = member.getRegTime();
        System.out.println("生日为"+regTime);
        memberDao.addMember(member);
    }

    //根据月份查询会员数量
    public List<Integer> findMemberCountByMonths(List<String> months) {
        List<Integer> memberCounts = new ArrayList<>();
        if(months != null && months.size() > 0){
            for (String month : months) {//2019.01
                String endTime = month + ".31";
                Integer memberCount = memberDao.findMemberCountBeforeDate(endTime);
                memberCounts.add(memberCount);
            }
        }
        return memberCounts;
    }

    //分页查询
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);//分页插件，会在执行sql之前将分页关键字追加到SQL后面
        Page<CheckItem> page = memberDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());

    }


    //根据id查询会员信息
    public Member findById(Integer id) {
        Member member = memberDao.findById(id);
        return member;
    }



    //编辑会员信息
    public void edit(Member member) {
        memberDao.edit(member);
    }



    //根据id删除会员(假删除  修改活跃属性)
    public void deleteById(Integer id) {

        int count = orderDao.findByMemberId(id);

        if (count==0){
            memberDao.falseDelete(id);
        }else {
            int a = 3/0;
        }


    }




    //通过会员ids查询详细信息并批量导出Excel
    public List<Member> findByMemberId(Integer[] ids) {

        //创建一个list集合存放每一个会员
        List<Member> list = new ArrayList<>();


        //遍历会员ids
        String address =null;
        Setmeal setmeal =null;

        for (Integer id : ids) {

            List<Map<String,Object>> list1 = orderDao.findOrderIdAndSetmealIDByMemberId(id);
            Member member = memberDao.findById(id);


            if (list1!=null && list1.size()>0) {


                for (Map<String, Object> map : list1) {

                    //遍历预约ID查询机构地址
                    Integer orderIds = (Integer) map.get("orderId");
                    address = memberDao.findAddressByOrderId(orderIds);
                    // addresses.add(address);
                    member.setAddress(address);

                    //遍历套餐ID查询套餐详情
                    Integer setmealIds = (Integer) map.get("setmealId");
                    setmeal = setmealDao.findById4Detail(setmealIds);
                    // setMeals.add(setmeal);
                    member.setSetMeals(setmeal);
                    list.add(member);
                }

            }else{
                list.add(member);
            }

        }
        return list;
    }
}
